package cn.jjxx.modules.sms.controller;


import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.framework.customutil.DateUtils;
import org.framework.customutil.sms.wnd.SMSBean;
import org.framework.customutil.sms.wnd.SMSHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.jjxx.core.common.controller.BaseCRUDController;
import cn.jjxx.core.model.AjaxJson;
import cn.jjxx.core.security.shiro.authz.annotation.RequiresPathPermission;
import cn.jjxx.core.utils.StringUtils;
import cn.jjxx.modules.sms.entity.SmsSendLog;
import cn.jjxx.modules.sms.service.ISmsSendLogService;

/**   
 * @Title: 短信发送日志
 * @Description: 短信发送日志
 * @author jeeweb
 * @date 2017-06-08 12:56:37
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("${admin.url.prefix}/sms/smssendlog")
@RequiresPathPermission("sms:smssendlog")
public class SmsSendLogController extends BaseCRUDController<SmsSendLog, String> {

	@Autowired
	private ISmsSendLogService smsSendLogService;
	
	@RequestMapping(value = "againSend", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public AjaxJson againSend(HttpServletRequest request, HttpServletResponse response,String ids){
		AjaxJson j = new AjaxJson();
		j.setMsg("补发成功");
		List<String> idsList = Arrays.asList(ids.split(","));
		List<SmsSendLog> list = smsSendLogService.selectBatchIds(idsList);
		SMSHelper helper = new SMSHelper();
		for(SmsSendLog log:list){
			if(log.getStatus().equals(String.valueOf(0))){
				SMSBean sms = helper.sendMsg(log.getPhone(), log.getSenddata(), null);
				if(StringUtils.isNotEmpty(sms.getErrid())){
					log.setStatus(String.valueOf(1));
					log.setMsg("短信于"+DateUtils.date2Str(DateUtils.datetimeFormat)+"时补发成功");
					smsSendLogService.insertOrUpdate(log);
				}else{
					j.setMsg(sms.getMsg());
					j.setRet(AjaxJson.RET_FAIL);
				}
			}
		}
		return j;
	}
}
